home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / glchess / ggz / chess.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  6.4 KB  |  172 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import struct
  5.  
  6. class GGZChessFeedback:
  7.     
  8.     def onSeat(self, seatNum, version):
  9.         pass
  10.  
  11.     
  12.     def onPlayers(self, whiteType, whiteName, blackType, blackName):
  13.         pass
  14.  
  15.     
  16.     def onClockRequest(self):
  17.         pass
  18.  
  19.     
  20.     def onClock(self, mode, seconds):
  21.         pass
  22.  
  23.     
  24.     def onStart(self):
  25.         pass
  26.  
  27.     
  28.     def onMove(self, move):
  29.         pass
  30.  
  31.  
  32.  
  33. class Chess:
  34.     CLOCK_NONE = 0
  35.     CLOCK_CLIENT = 1
  36.     CLOCK_SERVERLAG = 2
  37.     CLOCK_SERVER = 3
  38.     GGZ_SEAT_NONE = '\x00'
  39.     GGZ_SEAT_OPEN = '\x01'
  40.     GGZ_SEAT_BOT = '\x02'
  41.     GGZ_SEAT_PLAYER = '\x03'
  42.     GGZ_SEAT_RESERVED = '\x04'
  43.     GGZ_SEAT_ABANDONED = '\x05'
  44.     
  45.     def sendClock(self, mode, seconds):
  46.         return '\x04' + struct.pack('!I', mode << 24 | seconds & 16777215)
  47.  
  48.     
  49.     def sendMove(self, move, time = None):
  50.         cmd = '\x06' + struct.pack('!I', len(move)) + move
  51.         if time is not None:
  52.             cmd += struct.pack('!I', time)
  53.         
  54.         return cmd
  55.  
  56.     
  57.     def sendStart(self):
  58.         return '\x05'
  59.  
  60.     
  61.     def __init__(self, feedback):
  62.         self.feedback = feedback
  63.         self.decodeMethod = None
  64.         self.decodeMethods = {
  65.             '\x01': self.decodeSeat,
  66.             '\x02': self.decodePlayers,
  67.             '\x03': self.decodeClockRequest,
  68.             '\x04': self.decodeClock,
  69.             '\x05': self.decodeStart,
  70.             '\x07': self.decodeMove,
  71.             '\x08': self.decodeGameEnd,
  72.             '\n': self.decodeUpdate,
  73.             '\r': self.decodeDraw }
  74.  
  75.     
  76.     def decode(self, char):
  77.         if self.decodeMethod is None:
  78.             
  79.             try:
  80.                 self.decodeMethod = self.decodeMethods[char]
  81.             except KeyError:
  82.                 self.decodeMethod = None
  83.                 print 'Unknown data received: %s' % repr(char)
  84.                 return None
  85.  
  86.             self.command = ''
  87.         
  88.         self.command += char
  89.         self.decodeMethod()
  90.  
  91.     
  92.     def getGGZString(self, buffer):
  93.         if len(buffer) < 4:
  94.             return (None, 0)
  95.         (length,) = struct.unpack('!I', buffer[:4])
  96.         if len(buffer) < length + 4:
  97.             return (None, 0)
  98.         string = buffer[4:length + 4]
  99.         while string.endswith('\x00'):
  100.             string = string[:-1]
  101.             continue
  102.             len(buffer) < length + 4
  103.         return (string, length + 4)
  104.  
  105.     
  106.     def decodeSeat(self):
  107.         if len(self.command) == 3:
  108.             self.decodeMethod = None
  109.             (num, version) = struct.unpack('!xBB', self.command)
  110.             self.feedback.onSeat(num, version)
  111.         
  112.  
  113.     
  114.     def decodePlayers(self):
  115.         requiredLength = 2
  116.         if len(self.command) < requiredLength:
  117.             return None
  118.         whiteCode = self.command[1]
  119.         if len(self.command) < requiredLength:
  120.             return None
  121.         blackCode = self.command[requiredLength - 1]
  122.         if len(self.command) >= requiredLength:
  123.             self.decodeMethod = None
  124.             self.feedback.onPlayers(whiteCode, whiteName, blackCode, blackName)
  125.         
  126.  
  127.     
  128.     def decodeClockRequest(self):
  129.         self.decodeMethod = None
  130.         self.feedback.onClockRequest()
  131.  
  132.     
  133.     def decodeClock(self):
  134.         if len(self.command) == 5:
  135.             (value,) = struct.unpack('!xI', self.command)
  136.             mode = value >> 24
  137.             seconds = value & 16777215
  138.             self.feedback.onClock(mode, seconds)
  139.             self.decodeMethod = None
  140.         
  141.  
  142.     
  143.     def decodeStart(self):
  144.         self.decodeMethod = None
  145.         self.feedback.onStart()
  146.  
  147.     
  148.     def decodeMove(self):
  149.         (move, _) = self.getGGZString(self.command[1:])
  150.         if move is None:
  151.             return None
  152.         self.decodeMethod = None
  153.         self.feedback.onMove(move)
  154.  
  155.     
  156.     def decodeGameEnd(self):
  157.         if len(self.command) == 2:
  158.             self.decodeMethod = None
  159.         
  160.  
  161.     
  162.     def decodeUpdate(self):
  163.         if len(self.command) == 9:
  164.             self.decodeMethod = None
  165.         
  166.  
  167.     
  168.     def decodeDraw(self):
  169.         self.decodeMethod = None
  170.  
  171.  
  172.